home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / samples / midpnt / registry.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-14  |  3.7 KB  |  174 lines

  1. /*      Registry.cpp
  2.  *
  3.  * Registry handling class
  4.  *
  5.  * $Id: Registry.cpp 1.3 1997/01/14 17:42:08 pekangas Exp $
  6.  *
  7.  * Copyright 1996 Petteri Kangaslampi
  8. */
  9.  
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. #include "midasdll.h"
  13. #include "MidpNT.h"
  14. #include "registry.h"
  15.  
  16.  
  17. static void Error(char *msg)
  18. {
  19.     Panic(msg);
  20. }
  21.  
  22.  
  23. Registry::Registry()
  24. {
  25. }
  26.  
  27.  
  28. Registry::~Registry()
  29. {
  30. }
  31.  
  32.  
  33. int Registry::KeyExists(const char *name)
  34. {
  35.     LONG        err;
  36.  
  37.     err = RegOpenKeyEx(HKEY_CURRENT_USER, name, 0, KEY_ALL_ACCESS, &key);
  38.     if ( err != ERROR_SUCCESS )
  39.         return 0;
  40.     return 1;
  41. }
  42.  
  43.  
  44. void Registry::CreateKey(const char *name)
  45. {
  46.     LONG        err;
  47.     DWORD       createStatus;
  48.  
  49.     err = RegCreateKeyEx(HKEY_CURRENT_USER, name, 0, NULL,
  50.         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &createStatus);
  51.     if ( err != ERROR_SUCCESS )
  52.         Error("Registry key creation failed");
  53. }
  54.  
  55.  
  56.  
  57. void Registry::OpenKey(const char *name)
  58. {
  59.     LONG        err;
  60.  
  61.     err = RegOpenKeyEx(HKEY_CURRENT_USER, name, 0, KEY_ALL_ACCESS, &key);
  62.     if ( err != ERROR_SUCCESS )
  63.         Error("Registry key opening failed");
  64. }
  65.  
  66.  
  67. void Registry::Value(const char *name, void *data, DWORD *dataLength, DWORD
  68.     bufferLength, DWORD *dataType)
  69. {
  70.     LONG        err;
  71.     DWORD       len;
  72.     DWORD       type;
  73.  
  74.     len = bufferLength;
  75.     err = RegQueryValueEx(key, (LPSTR) name, 0, &type, (LPBYTE) data,
  76.         &len);
  77.     if ( err != ERROR_SUCCESS )
  78.     {
  79.         len = *dataLength;
  80.         type = *dataType;
  81.         err = RegSetValueEx(key, (LPSTR) name, 0, type,
  82.             (CONST BYTE*) data, len);
  83.         if ( err != ERROR_SUCCESS )
  84.             Error("Registry value creation failed");
  85.     }
  86.  
  87.     *dataLength = len;
  88.     *dataType = type;
  89. }
  90.  
  91.  
  92. void Registry::ValueString(const char *name, const char *defaultData,
  93.     char *dest, int bufferLength)
  94. {
  95.     DWORD       len;
  96.     DWORD       type = REG_SZ;
  97.  
  98.     if ( (unsigned) bufferLength < (strlen(defaultData) + 1) )
  99.         Error("Registry::ValueString() - too long default");
  100.  
  101.     strcpy(dest, defaultData);
  102.     len = strlen(dest) + 1;
  103.  
  104.     Value(name, (void*) dest, &len, bufferLength, &type);
  105.  
  106.     if ( type != REG_SZ )
  107.         Error("Registry::ValueString() - illegal value type");
  108. }
  109.  
  110.  
  111.  
  112. void Registry::WriteString(const char *name, const char *string)
  113. {
  114.     DWORD       err;
  115.  
  116.     err = RegSetValueEx(key, (LPSTR) name, 0, REG_SZ, (CONST BYTE*) string,
  117.         strlen(string)+1);
  118.     if ( err != ERROR_SUCCESS )
  119.         Error("Registry::WriteString(): value creation failed");
  120. }
  121.  
  122.  
  123.  
  124. DWORD Registry::ValueDWORD(const char *name, DWORD defaultData)
  125. {
  126.     DWORD       len, type, buf;
  127.  
  128.     buf = defaultData;
  129.     len = sizeof(DWORD);
  130.     type = REG_DWORD;
  131.  
  132.     Value(name, (void*) &buf, &len, sizeof(DWORD), &type);
  133.  
  134.     if ( type != REG_DWORD )
  135.         Error("Registy::ValueDWORD() - illegal value type");
  136.  
  137.     return buf;
  138. }
  139.  
  140.  
  141.  
  142. void Registry::WriteDWORD(const char *name, const DWORD data)
  143. {
  144.     DWORD       err;
  145.     DWORD       buf = data;
  146.  
  147.     err = RegSetValueEx(key, (LPSTR) name, 0, REG_DWORD, (CONST BYTE*) &buf,
  148.         sizeof(DWORD));
  149.     if ( err != ERROR_SUCCESS )
  150.         Error("Registry::WriteDWORD(): value creation failed");
  151. }
  152.  
  153.  
  154.  
  155. int Registry::ValueExists(const char *name)
  156. {
  157.     LONG        err;
  158.  
  159.     err = RegQueryValueEx(key, (LPSTR) name, 0, NULL, NULL, 0);
  160.     if ( err != ERROR_SUCCESS )
  161.         return 0;
  162.     return 1;
  163. }
  164.  
  165.  
  166. /*
  167.  * $Log: Registry.cpp $
  168.  * Revision 1.3  1997/01/14 17:42:08  pekangas
  169.  * Changed to use MIDAS DLL API
  170.  *
  171.  * Revision 1.2  1996/07/16 19:37:31  pekangas
  172.  * Fixed to compile with Visual C, converted to LFs and added RCS keywords
  173.  *
  174. */